// modules/recipe/recipe.controller.ts
import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  Put,
  Delete,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiBody,
  getSchemaPath,
} from '@nestjs/swagger';
import { RecipeService } from './recipe.service';
import { Recipe } from '../../entities/recipe.entity';

@ApiTags('recipes')
@Controller('recipes')
export class RecipeController {
  constructor(private readonly recipeService: RecipeService) {}

  @Get()
  @ApiOperation({
    summary: '获取所有食谱',
    description: '返回所有可用的食谱列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取食谱列表',
    schema: {
      type: 'array',
      items: { $ref: getSchemaPath(Recipe) },
    },
  })
  async getAllRecipes(
    @Query('skip') skip?: number,
    @Query('take') take?: number,
  ): Promise<Recipe[]> {
    return this.recipeService.getAllRecipes(skip, take);
  }

  @Get(':id')
  @ApiOperation({
    summary: '获取单个食谱',
    description: '根据ID获取特定食谱的详细信息',
  })
  @ApiParam({ name: 'id', description: '食谱ID', type: 'number' })
  @ApiResponse({
    status: 200,
    description: '成功获取食谱',
    schema: {
      allOf: [
        { $ref: getSchemaPath('ResponseResult') },
        {
          properties: {
            data: { $ref: getSchemaPath(Recipe) },
          },
        },
      ],
    },
  })
  @ApiResponse({ status: 404, description: '食谱未找到' })
  async findOne(@Param('id') id: number): Promise<Recipe> {
    return this.recipeService.findOne(id);
  }

  @Post()
  async create(
    @Body() recipeData: Partial<Recipe>,
  ): Promise<Recipe> {
    return this.recipeService.create(recipeData);
  }

  @Put(':id')
  async update(
    @Param('id') id: number,
    @Body() recipeData: Partial<Recipe>,
  ): Promise<Recipe> {
    return this.recipeService.update(id, recipeData);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: number): Promise<void> {
    return this.recipeService.remove(id);
  }
}
